﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Xiucai.Common;
using Xiucai.Common.Data;
using Xiucai.Common.Data.SqlServer;
using Xiucai.Common.Provider;
using Xiucai.BPM.Core.Model;

namespace Xiucai.BPM.Core.Dal
{
    public class DevStatusDal : BaseRepository<User>
    {
        public static DevStatusDal Instance
        {
            get { return SingletonProvider<DevStatusDal>.Instance; }
        }

        public int GetCBDDay(string devid)
        {
            int days = 0;
            string sql = "SELECT devid, MIN([updateTime]) as creatdate FROM [t_cbdstatus] where devID='" + devid + "' group by devid";
            DataTable ds = SqlEasy.ExecuteDataTable(sql);
            if (ds.Rows.Count == 1)
            {
                try
                {
                    DateTime dt = DateTime.Parse(ds.Rows[0]["creatdate"].ToString());
                    TimeSpan ts1 = new TimeSpan(dt.Ticks);
                    TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                    TimeSpan ts = ts1.Subtract(ts2).Duration();
                    days = ts.Days;
                }
                catch (Exception)
                {


                }
            }
            return days;
        }
        public int GetYBQDay(string devid)
        {
            int days = 0;
            string sql = "SELECT devid, MIN([updateTime]) as creatdate FROM [t_ybqstatus] where devID='" + devid + "' group by devid";
            DataTable ds = SqlEasy.ExecuteDataTable(sql);
            if (ds.Rows.Count == 1)
            {
                try
                {
                    DateTime dt = DateTime.Parse(ds.Rows[0]["creatdate"].ToString());
                    TimeSpan ts1 = new TimeSpan(dt.Ticks);
                    TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                    TimeSpan ts = ts1.Subtract(ts2).Duration();
                    days = ts.Days;
                }
                catch (Exception)
                {


                }
            }
            return days;
        }
        public int GetXQHDay(string devid)
        {
            int days = 0;
            string sql = "SELECT MIN(datetime) as creatdate  FROM [" + devid + "]";

            try
            {
                DataTable ds = DbUtils.GetXQH(sql);
                if (ds.Rows.Count == 1)
                {
                    DateTime dt = DateTime.Parse(ds.Rows[0]["creatdate"].ToString());
                    TimeSpan ts1 = new TimeSpan(dt.Ticks);
                    TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                    TimeSpan ts = ts1.Subtract(ts2).Duration();
                    days = ts.Days;
                }
            }
            catch (Exception)
            {


            }

            return days;
        }

    }
}
